import { createRouter, createWebHistory } from "vue-router";
import store from "@/store/index";
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: "/", redirect: "/login" },
    {
      path: "/home",
      component: () => import("../pages/home/index.vue"),
      children: [
        { path: "/home", redirect: "/home/workspace" },
        {
          path: "/admin",
          component: () => import("../pages/home/admin/index.vue"),
        },
        {
          path: "modetail",
          component: () => import("../pages/home/finance/modetail.vue"),
        },
        {
          path: "tools",
          component: () => import("../pages/home/finance/tools.vue"),
        },
        {
          path: "wages",
          component: () => import("../pages/home/finance/wages.vue"),
        },
        {
          path: "wagesdetail/:id",
          component: () => import("../pages/home/finance/wagesdetail.vue"),
          props: true,
        },
        {
          path: "workspace",
          component: () => import("../pages/home/workspace/index.vue"),
        },
        {
          path: "aftersale",
          component: () => import("../pages/home/aftersale/index.vue"),
        },
        {
          path: "furniture",
          component: () => import("../pages/home/commodity/furniture.vue"),
        },
        {
          path: "household",
          component: () => import("../pages/home/commodity/household.vue"),
        },
        {
          path: "discounts",
          component: () => import("../pages/home/discounts/index.vue"),
        },
        {
          path: "orderform",
          component: () => import("../pages/home/orderform/index.vue"),
        },
        {
          path: "staff",
          component: () => import("../pages/home/staff/index.vue"),
        },
      ],
    },
    {
      path: "/login",
      component: () => import("../pages/login/index.vue"),
    },
    {
      path: "/:pathMatch(.*)*",
      component: () => import("../pages/notfound/index.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem("token");
  if (to.path == "/login") {
    return next();
  }
  if (!token) {
    return next("/login");
  }
  // 获取当前路由地址，存入仓库
  store.commit("modifyUrl", to.path);
  next();
});

export default router;
